package cn.ilikes.tools.hbase.jpa.persistences.context.operations;

import cn.ilikes.tools.hbase.jpa.JPAHbasePersistenceProvider;
import cn.ilikes.tools.hbase.jpa.metadatas.IEntityMetaData;
import cn.ilikes.tools.hbase.jpa.metadatas.IEntityMetaDataFactory;
import cn.ilikes.tools.hbase.jpa.persistences.cache.Cache;
import cn.ilikes.tools.hbase.jpa.persistences.context.AbstractContext;
import cn.ilikes.tools.hbase.jpa.persistences.context.Operation.OperationType;

public abstract class ContextOperation {

	private OperationType operationType;

	private AbstractContext context;

	protected Cache cache;

	public ContextOperation(OperationType operationType, AbstractContext context) {
		super();
		this.operationType = operationType;
		this.context = context;
	}

	public Cache getCache() {
		return cache;
	}

	public String getPuName() {
		return context.getHbasePersistenUnitEnv().getPersistenceUnitsName();
	}

	public boolean init() {
		this.cache = context.getCache();

		return initSub(context);
	}

	public abstract boolean initSub(AbstractContext context);

	public OperationType getOperationType() {

		return operationType;
	}

	protected IEntityMetaData selectIEntityMetaData(String puName, Class<?> clazz) {

		IEntityMetaDataFactory entityMetaDataFactory = JPAHbasePersistenceProvider.findJPAHbasePersistenUnitEnv(puName).getEntityMetaDataFactory();

		return entityMetaDataFactory.selectIEntityMetaData(clazz);
	}

	public AbstractContext getContext() {
		return context;
	}
}